[message].page.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React from 'react';
  2. import {
  3. NextPage, GetServerSideProps, GetServerSidePropsContext,
  4. } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import { useRouter } from 'next/router';
  8. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  9. import {
  10. CommonProps, getServerSideCommonProps, getNextI18NextConfig,
  11. } from '~/pages/utils/commons';
  12. import {
  13. useCsrfToken,
  14. useCurrentPathname,
  15. } from '~/stores/context';
  16. type Props = CommonProps;
  17. const LoginPage: NextPage<CommonProps> = (props: CommonProps) => {
  18. // commons
  19. useCsrfToken(props.csrfToken);
  20. const { t } = useTranslation();
  21. const router = useRouter();
  22. const { message } = router.query;
  23. // page
  24. useCurrentPathname(props.currentPathname);
  25. const classNames: string[] = ['login-page'];
  26. let loginErrorElm;
  27. const renderResistrationSuccessFul = () => {
  28. return (
  29. <>
  30. <div className="alert alert-warning">
  31. <h2>{ t('login.registration_successful') }</h2>
  32. </div>
  33. <p>Wait for approved by administrators.</p>
  34. </>
  35. );
  36. };
  37. const renderSuspendedUserError = () => {
  38. return (
  39. <>
  40. <div className="alert alert-warning">
  41. <h2>{ t('login.sign_in_error') }</h2>
  42. </div>
  43. <p>This account is suspended.</p>
  44. </>
  45. );
  46. };
  47. const renderPasswordResetOrderError = () => {
  48. return (
  49. <>
  50. <div className="alert alert-warning mb-3">
  51. <h2>{ t('forgot_password.incorrect_token_or_expired_url') }</h2>
  52. </div>
  53. <a href="/forgot-password" className="link-switch">
  54. <i className="icon-key"></i> { t('forgot_password.forgot_password') }
  55. </a>
  56. </>
  57. );
  58. };
  59. const renderDefaultLoginError = () => {
  60. return (
  61. <div className="alert alert-warning">
  62. <h2>{ t('login.sign_in_error') }</h2>
  63. </div>
  64. );
  65. };
  66. switch (message) {
  67. case 'registered':
  68. loginErrorElm = () => renderResistrationSuccessFul();
  69. break;
  70. case 'suspended':
  71. loginErrorElm = () => renderSuspendedUserError();
  72. break;
  73. case 'password-reset-order':
  74. loginErrorElm = () => renderPasswordResetOrderError();
  75. break;
  76. default:
  77. loginErrorElm = () => renderDefaultLoginError();
  78. }
  79. return (
  80. <NoLoginLayout className={classNames.join(' ')}>
  81. <div className="mb-4 login-form-errors text-center">
  82. <div className='noLogin-dialog mx-auto'>
  83. <div className="col-12">
  84. {loginErrorElm()}
  85. </div>
  86. {/* If the transition source is "/login", use <a /> tag since the transition will not occur if next/link is used. */}
  87. <a href='/login'>
  88. <i className="icon-login mr-1" />{t('Sign in is here')}
  89. </a>
  90. </div>
  91. </div>
  92. </NoLoginLayout>
  93. );
  94. };
  95. /**
  96. * for Server Side Translations
  97. * @param context
  98. * @param props
  99. * @param namespacesRequired
  100. */
  101. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  102. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  103. props._nextI18Next = nextI18NextConfig._nextI18Next;
  104. }
  105. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  106. const result = await getServerSideCommonProps(context);
  107. // check for presence
  108. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  109. if (!('props' in result)) {
  110. throw new Error('invalid getSSP result');
  111. }
  112. const props: Props = result.props as Props;
  113. await injectNextI18NextConfigurations(context, props, ['translation']);
  114. return {
  115. props,
  116. };
  117. };
  118. export default LoginPage;